﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHospital.DAL;
using MyHospital.BLL;
namespace MyHospital.web
{
    public partial class BookCancel : System.Web.UI.Page
    {
        //建立MyHospital.DAL数据访问层中的MyHospitalDataContext类实例db
        MyHospitalDataContext db = new MyHospitalDataContext();
        BookService bookService = new BookService();
        protected void Page_Load(object sender, EventArgs e)
        {
            int patId = 0;
            if (Session["PatientID"] != null)
            {
                patId = (int)Session["PatientID"];
            }  
            var results = from b in db.BookingInfor
                          join d in db.Doctor on b.doctorId equals d.doctorId
                          join de in db.Department on d.departId equals de.departId
                          where b.patId == patId
                          select new
                          {
                              doctorName = d.doctorName,
                              departName = de.departName,
                              time = b.time,
                              date = b.date
                          };
            GridView1.DataSource = results;
            GridView1.DataBind();
            if (db.BookingInfor.Count() == 0)
            {
                Label1.Text = "暂无预约信息！";
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow row = btn.Parent.Parent as GridViewRow;
            String doctorName = row.Cells[0].Text;
            String time = row.Cells[2].Text;
            String date = row.Cells[3].Text;
            String[] times = time.Split('-');
            String dateNow = DateTime.Now.ToShortDateString().ToString();
            DateTime d1 = Convert.ToDateTime(date);
            DateTime d2 = Convert.ToDateTime(dateNow); //现在的日期
            int patId = (int)Session["PatientID"];
            int docId = bookService.findDoctorIdOfName(doctorName);
            if (DateTime.Compare(d2, d1) > 0)
            {
                Response.Write("<script>alert('该预约已失效！');window.location.href='BookCancel.aspx';</script>");
            }
            if (DateTime.Compare(d2, d1) < 0)
            {
                var results = from r in db.BookingInfor
                              where r.patId == patId && r.doctorId == docId && r.time == time && r.date == date
                              select r;
                db.BookingInfor.DeleteAllOnSubmit(results);
                db.SubmitChanges();
                bookService.bookCancelAddNumber(docId, time, d1);
                Response.Write("<script>alert('取消预约成功！');window.location.href='BookCancel.aspx';</script>");
            }
            if (DateTime.Compare(d2, d1) == 0)
            {
                String timeNow = DateTime.Now.ToShortTimeString().ToString();
                DateTime dt1 = Convert.ToDateTime(times[0]);
                DateTime dt2 = Convert.ToDateTime(timeNow);
                if (DateTime.Compare(dt2, dt1) > 0)
                {
                    Response.Write("<script>alert('该预约已失效！');window.location.href='BookCancel.aspx';</script>");
                }
                else
                {
                    TimeSpan ts1 = new TimeSpan(dt1.Ticks);
                    TimeSpan ts2 = new TimeSpan(dt2.Ticks);
                    TimeSpan ts = ts2.Subtract(ts1).Duration();
                    if (ts.Hours < 3)
                    {
                        Response.Write("<script>alert('只能在就诊时间前3个小时取消！');window.location.href='BookCancel.aspx';</script>");
                    }
                    else
                    {
                        var results = from r in db.BookingInfor
                                      where r.patId == patId && r.doctorId == docId && r.time == time && r.date == date
                                      select r;
                        db.BookingInfor.DeleteAllOnSubmit(results);
                        db.SubmitChanges();
                        bookService.bookCancelAddNumber(docId, time, d1);
                        Response.Write("<script>alert('取消预约成功！');window.location.href='BookCancel.aspx';</script>");
                    }
                }

            }

        }
    }
}